﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity.Infrastructure;
using BPM.Entity;

namespace BPM.Service
{
	//public class EntitiesInitializer : AlwaysRecreateDatabase<BarcodeContext>
	public class EntitiesInitializer : RecreateDatabaseIfModelChanges<BarcodeContext>
	{
		private BarcodeContext _db;

		protected override void Seed(BarcodeContext context)
		{
			base.Seed(context);

			this._db = context;

			this.InitBarcodeCategories();

			this.InitRoles();

			this.InitUsers();

			this.InitMaterialDefinitionCategories();
		}

		public void InitSysEntites()
		{
			if (SysUsers.Administrator == null)
			{
				if (this._db == null)
				{
					this._db = new BarcodeContext();
				}

				this.QueryCategories();
				this.QueryRoles();
				this.QueryUsers();
			}
		}

		#region 添加初始的BarcodeCategory

		private void InitBarcodeCategories()
		{
			SysBarcodeCategories.User = new BarcodeCategory()
			{
				Name = "用户条码"
			};
			SysBarcodeCategories.Role = new BarcodeCategory()
			{
				Name = "角色条码"
			};
			SysBarcodeCategories.MaterialDefinition = new BarcodeCategory()
			{
				Name = "物料条码"
			};
			SysBarcodeCategories.PurchaseOrder = new BarcodeCategory()
			{
				Name = "进货单条码"
			};
			SysBarcodeCategories.ProductAssemblyDefinition = new BarcodeCategory()
			{
				Name = "产品组装定义"
			};
			this._db.Set<BarcodeCategory>().Add(SysBarcodeCategories.User);
			this._db.Set<BarcodeCategory>().Add(SysBarcodeCategories.Role);
			this._db.Set<BarcodeCategory>().Add(SysBarcodeCategories.MaterialDefinition);
			this._db.Set<BarcodeCategory>().Add(SysBarcodeCategories.PurchaseOrder);
			this._db.Set<BarcodeCategory>().Add(SysBarcodeCategories.ProductAssemblyDefinition);
		}

		private void QueryCategories()
		{
			var categories = this._db.Set<BarcodeCategory>();
			SysBarcodeCategories.User = categories.First(c => c.Name == "用户条码");
			SysBarcodeCategories.Role = categories.First(c => c.Name == "角色条码");
			SysBarcodeCategories.MaterialDefinition = categories.First(c => c.Name == "物料条码");
			SysBarcodeCategories.PurchaseOrder = categories.First(c => c.Name == "进货单条码");
			SysBarcodeCategories.ProductAssemblyDefinition = categories.First(c => c.Name == "产品组装定义");
		}

		#endregion

		#region 添加初始的角色

		private void InitRoles()
		{
			SysRoles.Admin = new Role()
			{
				Barcode = this.NewBarcode(SysBarcodeCategories.Role),
				Name = "系统管理员"
			};
			SysRoles.Manager = new Role()
			{
				Barcode = this.NewBarcode(SysBarcodeCategories.Role),
				Name = "总经理"
			};
			SysRoles.Buyer = new Role()
			{
				Barcode = this.NewBarcode(SysBarcodeCategories.Role),
				Name = "采购员"
			};
			SysRoles.StorageManager = new Role()
			{
				Barcode = this.NewBarcode(SysBarcodeCategories.Role),
				Name = "库管员"
			};
			SysRoles.Supplier = new Role()
			{
				Barcode = this.NewBarcode(SysBarcodeCategories.Role),
				Name = "供货商"
			};
			SysRoles.MaterialStaff = new Role()
			{
				Barcode = this.NewBarcode(SysBarcodeCategories.Role),
				Name = "物料员"
			};
			SysRoles.AssembleManager = new Role()
			{
				Barcode = this.NewBarcode(SysBarcodeCategories.Role),
				Name = "组装组长"
			};
			SysRoles.AssembleStaff = new Role()
			{
				Barcode = this.NewBarcode(SysBarcodeCategories.Role),
				Name = "组装员"
			};
			var dbRoles = this._db.Set<Role>();
			var roles = new Role[]{
                SysRoles.Admin, SysRoles.AssembleManager, SysRoles.AssembleStaff,
                SysRoles.Buyer, SysRoles.Manager, SysRoles.MaterialStaff, SysRoles.StorageManager,
                SysRoles.Supplier
            };
			foreach (var role in roles)
			{
				dbRoles.Add(role);
			}
		}

		private void QueryRoles()
		{
			var entities = this._db.Set<Role>();
			SysRoles.Admin = entities.First(c => c.Name == "系统管理员");
			SysRoles.Manager = entities.First(c => c.Name == "总经理");
			SysRoles.Buyer = entities.First(c => c.Name == "采购员");
			SysRoles.StorageManager = entities.First(c => c.Name == "库管员");
			SysRoles.Supplier = entities.First(c => c.Name == "供货商");
			SysRoles.MaterialStaff = entities.First(c => c.Name == "物料员");
			SysRoles.AssembleManager = entities.First(c => c.Name == "组装组长");
			SysRoles.AssembleStaff = entities.First(c => c.Name == "组装员");
		}

		#endregion

		#region 添加初始的用户

		private void InitUsers()
		{
			SysUsers.Administrator = this.AddUser("admin",
				SysRoles.Admin, SysRoles.AssembleManager, SysRoles.AssembleStaff,
				SysRoles.Buyer, SysRoles.Manager, SysRoles.MaterialStaff,
				SysRoles.StorageManager, SysRoles.Supplier
				);

			this.AddUser("cg", SysRoles.Buyer);
			this.AddUser("kg", SysRoles.StorageManager);
			this.AddUser("wl", SysRoles.MaterialStaff);
			this.AddUser("jl", SysRoles.Manager);
			this.AddUser("zz", SysRoles.AssembleStaff);
			this.AddUser("zzzz", SysRoles.AssembleManager);
		}

		private User AddUser(string name, params Role[] roles)
		{
			var user = new User()
			{
				Barcode = this.NewBarcode(SysBarcodeCategories.User),
				IsValid = true,
				UserName = name,
				Password = "1",
				RealName = name,
				RoleList = new List<Role>(roles)
			};
			this._db.Set<User>().Add(user);
			return user;
		}

		private void QueryUsers()
		{
			SysUsers.Administrator = this._db.Set<User>().First(u => u.UserName == "admin");
		}

		#endregion

		private void InitMaterialDefinitionCategories()
		{
			var set = this._db.Set<MaterialDefinitionCategory>();

			var p1 = new MaterialDefinitionCategory()
			{
				Name = "1 耗材"
			};
			var p2 = new MaterialDefinitionCategory()
			{
				Name = "2 框架"
			};

			set.Add(p1);
			set.Add(p2);

			set.Add(new MaterialDefinitionCategory()
			{
				Name = "1.1 水泥",
				ParentCategory = p1
			});
			set.Add(new MaterialDefinitionCategory()
			{
				Name = "1.2 铜板",
				ParentCategory = p1
			});
			set.Add(new MaterialDefinitionCategory()
			{
				Name = "1.3 脚手架",
				ParentCategory = p1
			});
			set.Add(new MaterialDefinitionCategory()
			{
				Name = "2.1 底座",
				ParentCategory = p2
			});
			set.Add(new MaterialDefinitionCategory()
			{
				Name = "2.2 上底座",
				ParentCategory = p2
			});
			set.Add(new MaterialDefinitionCategory()
			{
				Name = "2.3 下底座",
				ParentCategory = p2
			});
		}

		private Barcode NewBarcode(BarcodeCategory category)
		{
			var result = new Barcode()
			{
				BarcodeCategory = category,
				Number = DateTime.Now.Ticks.ToString()
			};
			//this._db.Set<Barcode>().Add(result);
			//this._db.Save();
			return result;
		}
	}
}
